#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    long long countGood(vector<int>& nums, int k) {
        long long res = 0;
        unordered_map<int, int> hash;
        int n = nums.size();
        int left = 0, right = 0, same = 0;
        for (right = 0; right < n; right++)
        {
            same += hash[nums[right]]++;
            while (k <= same)
            {
                res += (n - right);
                same -= --hash[nums[left++]];
            }
        }
        return res;
    }
};